package com.maisonliberte.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.maisonliberte.enums.CrawlCategory;
import com.maisonliberte.enums.CrawlTarget;
import com.maisonliberte.enums.ImageSizeEnum;

public class ProductInfo {
	private String buyer = "";
	private CrawlCategory categoryInfo;
	private Map<ImageSizeEnum, List<ImageInfo>> imageList;
	private TableInfo tableInfo;
	private TableOtherInfo tableOther;
	private PriceInfo priceInfo;
	private String listImg;
	private CrawlTarget target;
	public CrawlTarget getTarget() {
		return target;
	}
	public void setTarget(CrawlTarget target) {
		this.target = target;
	}
	
	public String getBuyer() {
		return buyer;
	}
	public void setBuyer(String buyer) {
		this.buyer = buyer;
	}
	
	public String getListImg(){
		return listImg;
	}
	public void setListImg(String img){
		listImg = img;
	}
	
	public CrawlCategory getCategoryInfo() {
		return categoryInfo;
	}

	public String getCSVData(){
		//Url, Name, Name(JPN), Weight, Qty, Price, ConsumerPrice, ORG_Price, ORG_Carraige, Rate, EMS, FW_FEE, Commision, Spec
		String str =  String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", tableInfo.getSourceUrl(), 
				tableInfo.getName(), tableInfo.getName_jp(), tableInfo.getWeight(), tableInfo.getsQty(), tableInfo.getPrice(), 
				tableInfo.getConsumerPrice(), priceInfo.getOrgPrice(), priceInfo.getOrgCarraige(), priceInfo.getRate(), priceInfo.getEmsFee(), priceInfo.getForwardFee(), priceInfo.getCommision(), tableInfo.getExplan());
		return str;
	}
	
	public String getOtherInfoCSVData(){
		String str =  String.format(",%s,%s,%s,%s,%s", tableOther.getStaff_id(), tableOther.getStaff_comment(), tableOther.getMaker_id(), tableOther.getMaker_comment(), tableOther.getBrand_id());
		return str;
	}
	
	public void setCategoryInfo(CrawlCategory categoryInfo) {
		this.categoryInfo = categoryInfo;
	}


	public PriceInfo getPriceInfo() {
		return priceInfo;
	}

	public void setPriceInfo(PriceInfo priceInfo) {
		this.priceInfo = priceInfo;
	}

	public TableOtherInfo getTableOther() {
		return tableOther;
	}

	public void setTableOther(TableOtherInfo tableOther) {
		this.tableOther = tableOther;
	}

	public TableInfo getTableInfo() {
		return tableInfo;
	}

	public void setTableInfo(TableInfo tableInfo) {
		this.tableInfo = tableInfo;
	}

	public ProductInfo(){
		imageList = new HashMap<ImageSizeEnum, List<ImageInfo>>();
	}
	
	public List<ImageInfo> getImageListByKey(ImageSizeEnum key){
		return imageList.get(key);
	}
	
	public Map<String, String> getImagePath(ImageSizeEnum key){
		
		Map<String, String> maps = new HashMap<String, String>();
		
		for(ImageInfo info : imageList.get(key)){
			maps.put(info.getLocalPath(), info.getUploadPath());
		}
		
		return maps;
	}
	
	
	
	public Map<ImageSizeEnum, List<ImageInfo>> getImageList() {
		return imageList;
	}

	public void setImageList(Map<ImageSizeEnum, List<ImageInfo>> imageList) {
		this.imageList = imageList;
	}
	
	public String getNonDetailImageUrl(ImageSizeEnum imageEnum){
		if(imageList.get(imageEnum) != null){
			return imageList.get(imageEnum).get(0).getImgUrl();
		}
		return "";
	}
	
	private List<String> getOtherImageUrl(){
		List<String> imgUrls = new ArrayList<String>();
		for(ImageInfo img : imageList.get(ImageSizeEnum.OTHER_IMG)){
			imgUrls.add(img.getImgUrl());
		}
		return imgUrls;
	}
	
	private List<String> getDetailImageUrl(){
		List<String> imgUrls = new ArrayList<String>();
		for(ImageInfo img : imageList.get(ImageSizeEnum.DETAIL_IMG)){
			imgUrls.add(img.getImgUrl());
		}
		return imgUrls;
	}
	
	public String getOtherImage(){
		String img = "";
		for(String url : getOtherImageUrl()){
			img += String.format("%s|", url);
		}
		return img;
	}
	
	public String getDetailImage(){
		String img = "";
		for(String url : getDetailImageUrl()){
			img += String.format("%s|", url);
		}
		return img;
	}
	
	public String getDetailImageTag(){
		String img = "";
		for(String url : getDetailImageUrl()){
			img += String.format("<img src=%s><br />", url);
		}
		return img;
	}
	
	private String getStaffComment(){
		String table = "<div id=\"fromStaff\"><h3>Staff''s Comment</h3>{MSG}</div>";
		if(tableOther.getStaff_comment() != null){
			return table.replace("{MSG}", tableOther.getStaff_comment().replace("<h3>", "").replace("</h3>",  "").replace("직원보다", "").replace("'", "''"));	
		}else{
			return "";
		}
	}
	
	public Map<String, String> getSpec(){
		
		String tmp = tableInfo.getExplan();
		Map<String, String> goodInfo = new HashMap<String, String>();
		switch(this.target){
		case STYLE_STORE:
		case BD:
		case MINNE:	
			String[] list = tmp.split("■");
			for(int i = 0; i < list.length; i++){
				String[] item = list[i].split(":");
				if(item.length > 1){
					if(item[0] != null && item[0].trim().length() > 0){
						String specValue = "";
						for(int x = 1; x < item.length; x++){
							specValue += item[x];
						}
						
						goodInfo.put(item[0].replace("<br/>", "").replace("<br>", "").replace("<br />", "").replace("<br >", ""), specValue.replace("<br/>", "").replace("<br>", "").replace("<br />", "").replace("<br >", ""));
					}
				}else{
					if(item[0] != null && item[0].trim().length() > 0)
						goodInfo.put(item[0].replace("<br/>", "").replace("<br>", "").replace("<br />", "").replace("<br >", ""), "");
				}
			}
			break;
		default:
			break;
		}
		
		return goodInfo;
		
	}
	
	public String getContent(){
		
		String table = "<TABLE border=\"0\" width=\"100%\"><TBODY><TR><TD align=\"left\">{STAFF}</TD></TR></TBODY></TABLE>";
		table += "{IMG}";
		
		//table = table.replace("{STAFF}", getStaffComment()).replace("{SPEC}", getSpec()).replace("{IMG}", getDetailImageTag());
		table = table.replace("{STAFF}", getStaffComment()).replace("{SPEC}", "").replace("{IMG}", getDetailImageTag());
		
		return table.replace("사용의 컴퓨터 모니터", "사용하고 계신 컴퓨터 모니터");
		
	}
	
}
